<?php
	if(!defined('BASEPATH'))
		exit('No direct script access allowed');

	class m_Bills extends CI_Model
	{
		function __construct()
    	{
    	    // Call the Model constructor
    	    parent::__construct();
    	}

    	function get_user_id($userEmail)
    	{
			$this->load->database();
			$this->db->select('userId')->from('tblUser')->where('userEmail', $userEmail);
			$query = $this->db->get();
			return $query;
    	}
		
    	function get_vendor_name($Vendor_ID)
    	{
			$this->load->database();
			$this->db->select('Vendor_Name')->from('tblVendorNames ')->where('Vendor_ID', $Vendor_ID);
			$query = $this->db->get();
			return $query;
    	}
		
		function get_vendor_source($Vendor_ID)
    	{
			$this->load->database();
			$this->db->select('Vendor_Source')->from('tblVendorSources')->where('Vendor_ID', $Vendor_ID);
			$query = $this->db->get();
			return $query;
    	}
		
    	function getlinkedvendors($userId)
    	{
			$this->load->database();
			$this->db->select('AccountNumber, Vendor_ID')->from('tblVendorLinks')->where('userId', $userId);
			$query = $this->db->get();
			return $query;
    	}
		
		function insert_bill($bill, $Vendor_ID, $userId)
		{
			$this->load->database();
			$sql = "REPLACE INTO tblBills (invoiceNumber, AccountNumber, Name, Vendor, DueDate, Amount, Description, State, Vendor_ID, userId) VALUES ('" . $bill->invoiceNumber . "', '" . $bill->userID . "', '" . $bill->Name . "', '" . $bill->Vendor . "', '" . $bill->DueDate . "', '" . $bill->Amount . "', '" . $bill->Description . "', '" . $bill->State . "', '" . $Vendor_ID . "', '" . $userId . "')";
			$query = $this->db->query($sql);
			return $query;
		}
		
		function get_bills($userId)
		{
			$this->load->database();
			$this->load->library('Datatables'); 
			$this->datatables->select('Vendor, DueDate, Amount, Description, State, invoiceNumber, Vendor_ID')->from('tblBills')->where('userId', $userId)->where('State !=', 'Paid');
			$query = $this->datatables->generate();
			return $query;
		}
	}
?>
